package com.accesshollywood.models;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;

import javax.swing.table.AbstractTableModel;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.SQL.IndexedSQL;
import com.accesshollywood.grfxOrdering.basics.NamedList;
import com.accesshollywood.indexables.PreApprovedText;
import com.accesshollywood.indexables.User;
import com.accesshollywood.indexables.ViewFilterPreset;
import com.accesshollywood.p2p.P2P;

public class EditableOneColumnTableModel<T> extends AbstractTableModel {
	public boolean				isDirectList;
	public String				sqlTableName;
	public String				sqlEntryColumnName;
	public String				sqlIdColumnName;
	public String				sqlListTableName		= "";
	public String				sqlListEntryColumnName	= "";
	public String				sqlListIdColumnName		= "";
	public String				sqlListExtraData		= "";
	public int					listID;
	private String				extraData				= "";						// use for filenames for stills and footage lists
	protected LinkedHashSet<T>	data					= new LinkedHashSet<T>();

	public EditableOneColumnTableModel() {
		isDirectList = true;
	}

	public EditableOneColumnTableModel(String tableName, String columnName, String idName) {
		isDirectList = true;
		this.sqlTableName = tableName;
		this.sqlEntryColumnName = columnName;
		this.sqlIdColumnName = idName;
		// this.addColumn("");
	}

	public EditableOneColumnTableModel(String tableName, String columnName, String idName, String listTableName, String listColumnName, String listIdName, String listExtraData, int listID) {
		isDirectList = false;
		this.sqlTableName = tableName;
		this.sqlEntryColumnName = columnName;
		this.sqlIdColumnName = idName;

		this.sqlListTableName = listTableName;
		this.sqlListEntryColumnName = listColumnName;
		this.sqlListIdColumnName = listIdName;
		this.sqlListExtraData = listExtraData;
		this.listID = listID;

		// this.addColumn("");
	}

	public <T> ArrayList<T> toArrayList() {
		ArrayList<T> list = new ArrayList<T>();
		for (int i = 0; i < this.getRowCount(); i++) {
			list.add((T) this.getValueAt(i, 0));
		}
		return list;
	}

	public ArrayList<String> toWordList() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < this.getRowCount(); i++) {
//			String line = ((IndexedSQL<String>) this.getValueAt(i, 0)).element;
//			String[] words = line.split("\\s");
//			list.add(line);
//			for (String currWord : words) {
//				list.add(currWord);
//			}
		}
		return list;
	}

	public <T> void create(ArrayList<PreApprovedText> arrayList, ArrayList<User> permittedUsers) {
		data.clear();
		for (int i = 0; i < arrayList.size(); i++) {
			T currLine = (T) arrayList.get(i);
			addRow(new Object[] { currLine });
		}
	}

	public void create(IndexedSQL<String> newData, ArrayList<User> permittedUsers) {
		addRow(new Object[] { newData });
	}

	public void addRow(Object[] newData) {
		data.add((T) newData[0]);
	}

	public void p2pRefresh() {
		p2pRefreshList(listID);
	}

	public void p2pRefreshEntry(int idItemEntry) {
		if (!this.isDirectList) {
			int idList = GFXSQL.getInt(sqlListTableName, sqlListIdColumnName, this.sqlIdColumnName + "=" + idItemEntry)[0];
			p2pRefreshList(idList);
		} else {
			P2P.refreshPeers(new int[] { idItemEntry });
		}
	}

	public void p2pRefreshList(int idList) {
		int idItem = GFXSQL.getInt("item", "idItem", sqlListIdColumnName + "=" + idList)[0];
		int idItemList = GFXSQL.getInt("itemList", "idItemList", "idItem=" + idItem)[0];
		int idOrder = GFXSQL.getInt("gfxOrder", "idOrder", "idItemList=" + idItemList)[0];
		P2P.refreshPeers(new int[] { idOrder });
	}

	public synchronized String getExtraData() {
		return extraData;
	}

	public synchronized void setExtraData(String extraData) {
		this.extraData = extraData;
		GFXSQL.alterObject(sqlListTableName, sqlListExtraData, extraData, listID);
		p2pRefresh();
	}

	public synchronized void setExtraDataQuiet(String extraData) {
		this.extraData = extraData;
	}

	@Override
	public int getColumnCount() {
		return 1;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	public void setValueAt(Object aValue, int row, int column) {
		System.out.println(aValue);
		if (column == 0) {
			int i = 0;
			Iterator itr = data.iterator();
			while (itr.hasNext()) {
				Object curr = itr.next();
				if (i == row) {
//					((IndexedSQL) curr).setElement(aValue);
					return;
				}
				i++;
			}
		}
	}

	@Override
	public Object getValueAt(int row, int col) {
		if (col == 0) {
			int i = 0;
			Iterator itr = data.iterator();
			while (itr.hasNext()) {
				Object curr = itr.next();
				if (i == row) {
					return curr;
				}
				i++;
			}
		}
		return null;
	}

	public Object getElementAt(int i) {
		return getValueAt(i, 0);
	}

	public void addElement(Object newObj) {
		this.addRow(new Object[] { newObj });
	}
}
